# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=dcmtk
pkgver=3.6.7
_pkgver=${pkgver//./}
pkgrel=0
pkgdesc="OFFIS DICOM toolkit"
url="https://dcmtk.org/"
# armhf: compile fails with bad immediate value for offset
arch="all !armhf"
license="BSD-3-Clause"
makedepends="
	cmake
	libpng-dev
	libsndfile-dev
	libxml2-dev
	openjpeg-dev
	openssl-dev
	samurai
	tiff-dev
	zlib-dev
	"
install="$pkgname.pre-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc libdcmtk"
source="https://dicom.offis.de/download/dcmtk/dcmtk$_pkgver/dcmtk-$pkgver.tar.gz
	10-offile.patch
	20-thai.patch
	30-dont_export_all_executables.patch
	40-dcmqrscp.patch
	50-path_traversal_vuln.patch
	60-CVE-2022-43272.patch
	dcmtk.initd
	"

# secfixes:
#   3.6.7-r0:
#     - CVE-2022-43272

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# SKIP_RPATH=OFF to run test suite
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DBUILD_SHARED_LIBS=ON \
		-DDCMTK_WITH_OPENSSL=ON \
		-DDCMTK_ENABLE_PRIVATE_TAGS=ON \
		-DDCMTK_WITH_XML=ON \
		-DDCMTK_WITH_DOXYGEN=OFF \
		-DDCMTK_WITH_TIFF=ON \
		-DDCMTK_WITH_ZLIB=ON \
		-DDCMTK_WITH_ICONV=OFF \
		-DDCMTK_WITH_ICU=OFF \
		-DUSE_COMPILER_HIDDEN_VISIBILITY=ON \
		-DCMAKE_SKIP_RPATH=OFF \
		-DDCMTK_ENABLE_STL=ON \
		$crossopts
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# remove test binaries
	rm "$pkgdir"/usr/bin/*_tests
	rm "$pkgdir"/usr/bin/drttest
	rm "$pkgdir"/usr/bin/mkreport
	rm "$pkgdir"/usr/bin/msgserv
	rm "$pkgdir"/usr/bin/wltest
	rm "$pkgdir"/usr/share/dcmtk/SC.dump
	rm "$pkgdir"/usr/share/dcmtk/VLP.dump

	# remove changelog
	rm -rf "$pkgdir"/usr/share/doc

	# remove example modality worklist database and example queries
	rm -rf "$pkgdir"/usr/share/dcmtk/wlistdb "$pkgdir"/usr/share/dcmtk/wlistqry
}

openrc() {
	pkgdesc="$pkgdesc (OpenRC init scripts)"
	install -Dm755 "$srcdir"/dcmtk.initd "$subpkgdir"/etc/init.d/dcmtk
}

libdcmtk() {
	pkgdesc="OFFIS DICOM toolkit runtime libraries"
	amove usr/lib
	amove usr/share/dcmtk/*.dic
}

sha512sums="
b9c227f496de65424256636fa7ad266affc6593f72c6849271da94837b68c93a5b8e056ecd09d46550b6ac0530c406ab9f71751d317a5fd4171a222f9fc9ca45  dcmtk-3.6.7.tar.gz
14f9faa64c04435345cb46511f5d4e1c17fc000df2ddca330d28961b92a16b06dd9b689d331583009ac2d34e307d28c5bcaec0704f70329328b4100fc9efd042  10-offile.patch
97312ffc5b22dee1b9d6414f14f073e1f04d4b5013e118a07a2fd52938878a048bfee5b6b94756ec3f4b4a841dc12467395819a615565718b5b35e5df149a516  20-thai.patch
0306b39a16d76ff60e5d1badec34b1a831d9fdd1bea2c0bbfb6d203640470810801e0f0a3e5b34390284d210e01662384bf44c83adc44fd6eed1a737dc8d2bf6  30-dont_export_all_executables.patch
37b7867b379456aed71f15609dd57be142984b0430605eba9771d2766817ac95e6526759b105686a76df28f09e49ee5e8907f46c38b4d7de947278b0faaf7568  40-dcmqrscp.patch
0b0544ed65465f7c5afe6d66bceb752101cf5a4386126816e7721ecead05af17b705a263c652717769fcc9b86e1c9dac1e48bd66492b4ad3d07cf2266ab56fd8  50-path_traversal_vuln.patch
a712babbac31c8e858447cb8a99e5af19dc5847392e0c13ca74a9e72a6a22473aef82dfe88baf680a8c6b97a5d37415ececc3cae0ceabe334dd5504aeb3073ee  60-CVE-2022-43272.patch
dd8e24011178f3b5fa35150439aea2640a6f9da702da883a61e6eeb835223ee6967e4586f4e75b004e04da0f061c1edbf6d00a252480c64142a68b3213810aad  dcmtk.initd
"
